package com.etop.learnapp.fragment;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.etop.learnapp.R;
import com.etop.learnapp.activity.PicActivity;
import com.etop.learnapp.util.BitmapCreate;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by John on 2015/10/26 0026.
 */
public class DetailFragment extends Fragment {
    LinearLayout mLienarLayout;
    List<ImageView> list ;
    private  StringBuilder sb;
    private String ImageUri;
    private String Uri;
    private View rootView;
    ImageView iv;

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container , Bundle saveInstanceState){
        rootView = inflater.inflate(R.layout.fragment_detail  ,container,false);
        mLienarLayout = (LinearLayout) rootView.findViewById(R.id.qwe);
        list = new ArrayList<>();
        int chapterSelect = getActivity().getIntent().getIntExtra("chapterSelect", -1);
        int sectionSelect = getActivity().getIntent().getIntExtra("sectionSelect", -1);
        int keySelect = getActivity().getIntent().getIntExtra("keySelect", -1);

        String fileName = "cp" + chapterSelect + "_" + sectionSelect + "_" + keySelect + ".txt";
        ImageUri = "picture/"+chapterSelect + "_" + sectionSelect + "_" + keySelect+"/detail";
        AssetManager am = getActivity().getAssets();
        try {
            InputStream is = am.open(fileName);
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line;
            sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line);
                sb.append("\n");
            }
            sb.append("\n");

        }catch (IOException e) {
            System.out.println("IOException");
        }
        Context mContext = mLienarLayout.getContext();
        int num=0;
        try {
            num = am.list(ImageUri).length;
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (int i=1;i<=num;i++){
            Uri = "assets://"+ImageUri+"/"+i+".jpg";
            iv = new ImageView(mContext);
            iv.setScaleType(ImageView.ScaleType.FIT_START);
            iv.setAdjustViewBounds(true);
            new BitmapCreate(Uri,mContext,iv);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            lp.setMargins(5,10,5,10);
            iv.setLayoutParams(lp);

            iv.setOnClickListener(new View.OnClickListener() {
                                      String UR = Uri;
                                      @Override
                                      public void onClick(View v) {
                                          Intent intent = new Intent(getActivity(), PicActivity.class);
                                          intent.putExtra("Resources", UR);
                                          startActivity(intent);
                                      }
                                  }
            );
            list.add(iv);
            mLienarLayout.addView(iv);
        }
        EditText it = new EditText(mContext) ;
        it.setFocusable(false);
        it.setText(sb.toString());
        it.setBackground(null);
        mLienarLayout.addView(it);
        return rootView;
    }




}
